<?php /* DEPARTMENTS $Id: view.php,v 1.21.6.4 2007/01/03 18:59:16 gregorerhardt Exp $ */
global $department, $min_view;
$dept_id = isset($_GET['dept_id']) ? $_GET['dept_id'] : (isset($department) ? $department : 0);

// check permissions
$canRead = !getDenyRead( $m, $dept_id );
$canEdit = !getDenyEdit( $m, $dept_id );

if (!$canRead) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

if (isset( $dept_id ) && $dept_id >0) {
	$AppUI->setState( 'DeptIdxDepartment', $dept_id );
}
$dept_id = $AppUI->getState( 'DeptIdxDepartment' ) !== NULL ? $AppUI->getState( 'DeptIdxDepartment' ) : ($AppUI->user_department > 0 ? $AppUI->user_department : $company_prefix.$AppUI->user_company);

if (isset( $_GET['tab'] )) {
	$AppUI->setState( 'DeptVwTab', $_GET['tab'] );
}
$tab = $AppUI->getState( 'DeptVwTab' ) !== NULL ? $AppUI->getState( 'DeptVwTab' ) : 0;

if ($dept_id > 0) {
	// pull data
	$q  = new DBQuery;
	$q->addTable('companies', 'com');
	$q->addTable('departments', 'dep');
	$q->addQuery('dep.*, company_name');
	$q->addQuery('con.contact_first_name');
	$q->addQuery('con.contact_last_name');
	$q->addJoin('users', 'u', 'u.user_id = dep.dept_owner');
	$q->addJoin('contacts', 'con', 'u.user_contact = con.contact_id');
	$q->addWhere('dep.dept_id = '.$dept_id);
	$q->addWhere('dep.dept_company = company_id');
	$sql = $q->prepare();
	$q->clear();
}
if (!db_loadHash( $sql, $dept )) {
	$AppUI->setMsg('Invalid Department ID', UI_MSG_ERROR);
	$AppUI->redirect();
} elseif ($dept_id <= 0) {
	$AppUI->setMsg('Please choose a Department first!', UI_MSG_WARNING);
	$AppUI->redirect();
} else {
	$company_id = $dept['dept_company'];
	if (!$min_view) {
		// setup the title block
		$titleBlock = new CTitleBlock( 'Department Information' );
		if ($canEdit) {
			$titleBlock->addCell();
			$titleBlock->addButton($AppUI->_('new department'),
				'index.php?m=departments&a=addedit&company_id='.$company_id.'&dept_parent='.$dept_id);
		}
		$titleBlock->addButton('company list', 'index.php?m=companies');
		$titleBlock->show();
	}
	$AppUI->savePlace();
?>
<script language="javascript">
<? if ($canDelete) { ?>
function delIt() {
	if (confirm( "<?=$AppUI->_('departmentDelete', UI_OUTPUT_JS)?>" )) {
		document.frmDelete.submit();
	}
}
<? } ?>
</script>

<form name="frmDelete" action="index.php?m=departments" method="post">
	<input type="hidden" name="dosql" value="do_dept_aed" />
	<input type="hidden" name="del" value="1" />
	<input type="hidden" name="dept_id" value="<?=$dept_id?>" />
</form>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td class="tabon"><?=$dept['dept_name']?></td>
<td width="100%">&nbsp;</td>
</tr>
</table>
<table border="0" cellpadding="7" cellspacing="0" width="100%" class="infopanel">
<tr>
	<td class="status"><?=$AppUI->_('Company')?>:</td>
	<td nowrap="nowrap"><a href="?m=companies&a=view&company_id=<?=$company_id?>"><?=$dept['company_name']?></a></td>
	<td class="status"><?=$AppUI->_('Owner')?>:</td>
	<td nowrap="nowrap"><a href="javascript:User(<?=$dept['dept_owner']?>).show()"><?=@$dept['contact_first_name'].' '.@$dept['contact_last_name']?></a></td>
	<td align="right" width="100%">
	<?
		if ($canEdit) {
			showButton('edit', '?m=departments&a=addedit&dept_id='.$dept_id);
			if ($canDelete) showButton( 'delete', 'js:delIt()' );
		}
	?>
	</td>
</tr>
<table>
<table border="0" cellpadding="4" cellspacing="0" width="100%" class="subinfopanel">
<tr valign="top">
	<td width="50%">
		<table cellspacing="1" cellpadding="2" border="0" width="100%" class="list">
		<tr>
			<td colspan="2"><b><?=$AppUI->_('Contact information')?></b></td>
		</tr>
		<tr class="second">
			<td align="right" nowrap><?=$AppUI->_('Phone')?></td>
			<td width="100%"><?=@$dept['dept_phone']?></td>
		</tr>
		<tr class="second">
			<td align="right" nowrap><?=$AppUI->_('Fax')?></td>
			<td width="100%"><?=@$dept['dept_fax']?></td>
		</tr>
		<tr class="second" valign="top">
			<td align="right" nowrap><?=$AppUI->_('Address')?></td>
			<td>
			<?php
				@$dept['dept_address1']
					.(isset($dept['dept_city']) ? ', '.$dept['dept_city'] : '')
					.(isset($dept['dept_state']) ? ', '.$dept['dept_state'] : '')
					.(isset($dept['dept_zip']) ? ', '.$dept['dept_zip'] : '')
			?>
			</td>
		</tr>
		</table>
	</td>
	<td width="50%" height="100%">
		<table cellspacing="1" cellpadding="2" border="0" width="100%" class="list" height="100%">
		<tr>
			<td><b><?=$AppUI->_('Description')?></b></td>
		</tr>
		<tr class="second" height="100%">
			<td width="100%"><?=str_replace( chr(10), '<br/>', $dept['dept_desc'])?>&nbsp;</td>
		</tr>
		</table>
	</td>
</tr>
</table>
<?php
	// tabbed information boxes
	$tabBox = new CTabBox( '?m=departments&a='.$a.'&dept_id='.$dept_id, $tab );
	$tabBox->add('vw_contacts', 'Contacts');
	// include auto-tabs with 'view' explicitly instead of $a, because this view is also included in the main index site
	$tabBox->loadExtras($m, 'view');
	$tabBox->show();
}
?>
